<?php
/**
 * 投票
 * Enter description here ...
 * @author wolf
 *
 */
class GglController extends Action
{
    
    //验证是否为微信浏览器
    public function _routeDown()
    {
        if (! strpos($_SERVER['HTTP_USER_AGENT'], "MicroMessenger")) {
            // echo "请使用微信登录,please use weixin.thanks";
        

        // exit();
        }
    }
    /**
     * 配置要参与的企业
     * 
     */
    public function gailv()
    {
        $num = rand(1, 200);
        $prices = "谢谢惠顾";
        switch ($num) {
            case $num < 5 :
                $prices = "300元代金券";
                break;
            case $num >= 5 && $num < 15 :
                $prices = "200元代金券";
                break;
            case $num >= 15 && $num < 30 :
                $prices = "100元代金券";
                break;
            case $num >= 30 && $num < 50 :
                $prices = "50元代金券";
                break;
            default :
                $prices = "谢谢惠顾";
                break;
        }
        return $prices;
    }
    
    //礼品
    public function present()
    {
        $prices = $this->gailv();
        
        $value = array(
                'status' => true,
                'prices' => $prices);
        $rs = VoteModel::instance()->getWxUser(array(
                'username' => $_SESSION['openid']));
        if ($rs['num'] < 1) {
            $value = array(
                    'status' => true,
                    'prices' => "抽奖次数已用完");
            echo json_encode($value);
            exit();
        }
        if ($prices != "谢谢惠顾") {
            $this->setLuck($_SESSION['openid'], $prices);
        }
        VoteModel::instance()->saveWxUser(array(
                'num' => $rs['num'] - 1), array(
                'username' => $_SESSION['openid']));
        
        echo json_encode($value);
    
    }
    /**
     * 投票首页
     * 
     */
    public function index()
    {
        if (isset($_GET['openid'])) {
            // echo $_GET['u'];
            $user = VoteModel::instance()->getWxUser(array(
                    'biaoji' => trim($_GET['openid'])));
        
        }
        $_SESSION['openid'] = $user['username'];
        $listme = VoteModel::instance()->getLuckByWhere(array(
                'username' => $user['username']));
        $listall = VoteModel::instance()->getLuckByWhere(NULL, null, "id DESC", 20);
        $zj = $this->gailv();
        if ($zj <= 10) {
            //    $this->setLuck($user['username'], $zj);
        }
        
        $this->view()->assign('user', $user);
        $this->view()->assign('listall', $listall);
        
        $this->view()->assign('list', $listme);
        //设置中奖概率
        $this->view()->clearCompiledTemplate("mysql:ggl.tpl");
        $this->view()->clearCache("mysql:ggl.tpl");
        $this->view()->display("mysql:ggl.tpl");
    
    }
    public function setLuck($username, $remark)
    {
        $time = time();
        VoteModel::instance()->addLuck(array(
                'username' => $username,
                'add_time' => $time,
                'remark' => $remark));
    }
}